package com.lai.note.service.impl;

import com.lai.note.dao.CopyWritingMapper;
import com.lai.note.model.CopyWriting;
import com.lai.note.service.CopyWritingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Random;


@Service
public class CopyWritingServiceImpl implements CopyWritingService {

    @Autowired
    private CopyWritingMapper copyWritingMapper;
    static Random random = new Random();

    @Override
    public boolean userAddNew(String userId,String content) {
        CopyWriting record = new CopyWriting();
        record.setWriteId(copyWritingMapper.getWritingCount()+1);
        record.setContent(content);
        record.setVerifyStatus(false);
        return copyWritingMapper.addNew(record)==1 && copyWritingMapper.addNewUserAndCopyWriting(userId,record.getWriteId())==1;
    }

    @Override
    public boolean adminAddNew(String userId,String content) {
        CopyWriting record = new CopyWriting();
        record.setWriteId(copyWritingMapper.getWritingCount()+1);
        record.setContent(content);
        record.setVerifyStatus(true);
        return copyWritingMapper.addNew(record)==1 && copyWritingMapper.addNewUserAndCopyWriting(userId,record.getWriteId())==1;
    }

    @Override
    public boolean adminDeleteCopyWriting(int writeId) {
        return copyWritingMapper.adminDeleteCopyWriting(writeId)==1
                && copyWritingMapper.adminDeleteUserAndCopyWriting(writeId)==1;
    }

    @Override
    public CopyWriting randomGenerate() {
        int id = random.nextInt(copyWritingMapper.getWritingCount())+1;
        return copyWritingMapper.selectByRandomId(id);
    }

    @Override
    public List<CopyWriting> selectUserAllCopyWriting(String userId) {
        return copyWritingMapper.selectUserAllCopyWriting(userId);
    }
}
